package com.emar.adx.yiqifa.dao;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;

import javax.sql.DataSource;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by Administrator on 2015/11/17.
 */
public class BaseSqlDAO {

    private Logger log = LoggerFactory.getLogger(BaseSqlDAO.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    /**
     * @param namedSql
     * @param javaBean
     * @return
     */
    protected int update(String namedSql, Object javaBean) {
        SqlParameterSource paramSource = new BeanPropertySqlParameterSource(
                javaBean);
        return this.jdbcTemplate.update(namedSql, paramSource);
    }



    /**
     * @param sql
     * @param paramValue
     * @return
     */
    protected int commonUpdate(String sql, Object... paramValue) {
        return this.jdbcTemplate.update(sql, paramValue);
    }
    /**
     * @param sql
     * @return
     */
    protected int getCount(String sql) {
        return this.jdbcTemplate.queryForObject(sql, Integer.class);
    }
    /**
     * @param sql
     * @param returnType
     * @return
     */
    protected <T> T getJavaBean(String sql, Class<T> returnType) {
        RowMapper<T> rowMapper = new BeanPropertyRowMapper<T>(returnType);
        try {
            return this.jdbcTemplate.queryForObject(sql, rowMapper);
        } catch (Exception ex) {
            return null;
        }
    }
    /**
     * @param sql
     * @param returnType
     * @return
     */
    protected <T> List<T> getList(String sql, Class<T> returnType) {
        RowMapper<T> rowMapper = new BeanPropertyRowMapper<T>(returnType);
        log.info(sql);
        return this.jdbcTemplate.query(sql, rowMapper);
    }
    protected <T> List<T> getList(String sql, Class<T> returnType,
                                  Object... paramValue) {
        RowMapper<T> rowMapper = new BeanPropertyRowMapper<T>(returnType);
        return this.jdbcTemplate.query(sql, rowMapper, paramValue);
    }
    /**
     * @param returnType
     * @return
     */
    protected <T> List<T> getPageList(String querySQL, Class<T> returnType,
                                      int pageNumber, int pageSize) {
        RowMapper<T> rowMapper = new BeanPropertyRowMapper<T>(returnType);
        StringBuffer sqlBuffer = new StringBuffer();
        sqlBuffer.append("select * from ( ");
        sqlBuffer.append(querySQL);
        sqlBuffer.append(" ) as t limit " + pageNumber + "," + pageSize);
        log.info(sqlBuffer.toString());
        return this.jdbcTemplate.query(sqlBuffer.toString(), rowMapper);
    }
}
